package com.dimong.www.loan.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dimong.www.loan.R;

public class ActionBarView extends FrameLayout implements OnClickListener {
    private ImageView mLeftImg;
    private ImageView mRightImg;
    private TextView mTitle;
    private RelativeLayout title_bg;
    private View bar_line_view;
    private TextView title_right;
    private OnLeftImgClickListener mOnLeftImgClickListener;
    private OnRightImgClickListener mOnRightImgClickListener;
    private OnTitleClickListener mOnTitleClickListener;
    private OnRightTextClickListener onRightTextClickListener;
    public interface OnLeftImgClickListener {
        void onClick(View img);
    }

    public interface OnRightImgClickListener {
        void onClick(View img);
    }

    public interface OnTitleClickListener {
        void onClick(View img);
    }
    public interface OnRightTextClickListener {
        void onClick(View img);
    }
    public ActionBarView(Context context) {
        this(context, null);
    }

    public ActionBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ActionBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (isInEditMode()) {
            return;
        }
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_action_bar, this, true);
        mLeftImg = findViewById(R.id.left_img);
        mLeftImg.setOnClickListener(this);
        mRightImg = findViewById(R.id.right_img);
        mRightImg.setVisibility(View.INVISIBLE);
        mRightImg.setOnClickListener(this);
        mTitle = findViewById(R.id.title_text);
        title_bg = findViewById(R.id.title_bg);
        mTitle.setVisibility(View.GONE);
        mTitle.setOnClickListener(this);
        bar_line_view = findViewById(R.id.bar_line_view);
        title_right = findViewById(R.id.title_right);
        title_right.setOnClickListener(this);
        title_right.setVisibility(GONE);
    }

    public void setTitle(String text) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setCompoundDrawables(null, null, null, null);
        if (null != text && !"".equals(text)) {
            mTitle.setText(text);
        }
    }

    public void setTitle(SpannableStringBuilder text) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setCompoundDrawables(null, null, null, null);
        if (null != text && !"".equals(text)) {
            mTitle.setText(text);
        }
    }

    public void setTitleColor(int color) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setTextColor(color);
    }

    public void setBackgroundColor(int color) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setTextColor(color);
    }

    public void setTitle(String text, OnTitleClickListener listener) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setCompoundDrawables(null, null, null, null);
        if (null != text && !"".equals(text)) {
            mTitle.setText(text);
            mOnTitleClickListener = listener;
        }
    }

    public void setLeftImg(OnLeftImgClickListener listener) {
        setLeftImg(R.drawable.back, listener);
    }

    public void setLeftImg(int resId, OnLeftImgClickListener listener) {
        mLeftImg.setImageResource(resId);
        mLeftImg.setVisibility(View.VISIBLE);
        mOnLeftImgClickListener = listener;
    }

    public void setTitle(int id, String text) {
        Drawable img_left;
        img_left = getResources().getDrawable(id);
        img_left.setBounds(0, 0, 100, 100);
        mTitle.setCompoundDrawables(null, null, img_left, null);
        mTitle.setText(text);
        mTitle.setVisibility(View.VISIBLE);
    }

    public void setTitle(int id, String text, OnTitleClickListener listener) {
        Drawable img_left;
        img_left = getResources().getDrawable(id);
        img_left.setBounds(0, 0, 100, 100);
        mTitle.setCompoundDrawables(null, null, img_left, null);
        mTitle.setText(text);
        mTitle.setVisibility(View.VISIBLE);
        mOnTitleClickListener = listener;
    }

    public void setTitle(int stringID) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText(stringID);
    }

    public String getTitle() {
        return mTitle.getText().toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_img:
                if (mOnLeftImgClickListener != null)
                    mOnLeftImgClickListener.onClick(v);
                break;
            case R.id.right_img:
                if (mOnRightImgClickListener != null)
                    mOnRightImgClickListener.onClick(v);
                break;
            case R.id.title_text:
                if (mOnTitleClickListener != null) {
                    mOnTitleClickListener.onClick(v);
                }
                break;
            case R.id.title_right:
                if (onRightTextClickListener != null) {
                    onRightTextClickListener.onClick(v);
                }
                break;
            default:
                break;
        }
    }

    public void setLeftImgVisible(int visible){
        mLeftImg.setVisibility(visible);
    }

    public void setOnRightText(String text,OnRightTextClickListener onRightTextClickListener) {
        title_right.setVisibility(VISIBLE);
        title_right.setText(text);
        this.onRightTextClickListener = onRightTextClickListener;

    }

    public void setBottomLineGONE() {
        bar_line_view.setVisibility(View.GONE);
    }
}
